﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using util;

namespace WebApplication1.dormitoryAdministrator
{
    public partial class RepairReport : System.Web.UI.Page
    {
        String hostel;
        protected void Page_Load(object sender, EventArgs e)
        {
            hostel = (String)Application["hostel"];
            SqlDataSource1.SelectCommand = "SELECT * FROM `dormitory_maintenance` where hostel = '" + hostel + "'";
            this.init();
        }

        public void init() {

            int count = GridView1.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                String state = GridView1.Rows[i].Cells[7].Text;
                if (state == "已处理")
                {
                    Button process = GridView1.Rows[i].Cells[8].FindControl("process") as Button;
                    process.Visible = false;
                }

            }
        }

        protected void process_Click(object sender, EventArgs e)
        {
            Button but = (Button)sender;
            //如果用的是Button： Button but=(Button)sender
            GridViewRow gvr = (GridViewRow)but.NamingContainer;
            int row = gvr.RowIndex;
            string id = this.GridView1.DataKeys[row]["id"].ToString();
            String sql = "update dormitory_maintenance set state = '已处理' where id = " + id;
            DataTable data = MySqlHelper.GetDataTable(sql);
            Response.AddHeader("Refresh", "0");
            this.init();
        }

        protected void processed_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM `dormitory_maintenance` where state = '已处理';";
            DataTable data = MySqlHelper.GetDataTable(sql);
            GridView1.DataSource = data;
            GridView1.DataSourceID = null;
            GridView1.DataBind();
            this.init();
        }

        protected void unprocess_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM `dormitory_maintenance` where state = '未处理';";
            DataTable data = MySqlHelper.GetDataTable(sql);
            GridView1.DataSource = data;
            GridView1.DataSourceID = null;
            GridView1.DataBind();
            this.init();

        }
          

        protected void selectAll_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = null;
            GridView1.DataSourceID = "SqlDataSource1";
            GridView1.DataBind();
            this.init();

        }

        protected void select_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM `dormitory_maintenance` where name = '" + selectName.Text + "'";
            DataTable data = MySqlHelper.GetDataTable(sql);
            GridView1.DataSource = data;
            GridView1.DataSourceID = null;
            GridView1.DataBind();
            this.init();
        }
    }
}